package com.arvinclub.web.controller.admin;

import com.arvinclub.model.entity.User;
import com.arvinclub.service.service.UserService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 管理员相关
 */
@Controller
public class AdminController {

    @Resource
    private UserService userService;

    /**
     * 管理员页面,默认第一页
     */
    @GetMapping("admin/manager.html")
    public String admin(Model model, @RequestParam(required = false) String username) {
        return admin(model, 1, username);
    }

    /**
     * 管理员页面,分页
     */
    @GetMapping("admin/manager.html/{page}")
    public String admin(Model model, @PathVariable int page, @RequestParam(required = false) String username) {
        /*根据页号获取数据*/
        username = StringUtils.isEmpty(username) ? null : username.trim();
        PageInfo<User> userPage = userService.selectAllUser(page, username);
        model.addAttribute("userPage", userPage);
        return "admin/list";
    }

    /**
     * 删除用户
     *
     * @param userId userId
     * @param page page
     */
    @PostMapping("admin/delUser/{userId}/{page}")
    public String delUser(@PathVariable int userId, @PathVariable String page) {
        userService.delUser(userId);
        return "redirect:/admin/manager.html/" + page;
    }

    /**
     * 恢复用户
     *
     * @param userId userId
     * @param pages  page
     */
    @PostMapping("admin/reuseUser/{userId}/{pages}")
    public String reuseUser(@PathVariable int userId, @PathVariable String pages) {
        userService.reuseUser(userId);
        return "redirect:/admin/manager.html/" + pages;
    }

    /**
     * 禁言用户
     *
     * @param userId userId
     * @param days   天数
     * @param pages  pages
     */
    @PostMapping("admin/mute/{userId}/{days}/{pages}")
    public String mute(@PathVariable int userId, @PathVariable int days, @PathVariable String pages) {
        userService.mute(userId, days);
        return "redirect:/admin/manager.html/" + pages;
    }

}
